//
//  DDNotificationMessageViewController.m
//  DingDongZP
//
//  Created by 王威 on 16/3/18.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDNotificationMessageViewController.h"
#import "DDTitlePullView.h"
#import "DDNotificationMessageCell.h"
#import "DDJobfinderNotification.h"

@interface DDNotificationMessageViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (strong, nonatomic) NSString          *titleString;
@property (strong, nonatomic) UILabel           *titleTouchLabel;
@property (strong, nonatomic) DDTitlePullView   *titlePullView;
@property (strong, nonatomic) UIImage           *pullImage;
@property (strong, nonatomic) UITableView       *notificationTableView;

@property (nonatomic, assign) DDJobfinderNotificationType notificationType;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL hasMore;
@property (nonatomic, strong) NSMutableArray *notifications;

@end

@implementation DDNotificationMessageViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setViews];
    
    [self SetNavgationBarTitle];
    
//    [self reloadData];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.rdv_tabBarController setTabBarHidden:true animated:true];
}

- (void)viewDidLayoutSubviews
{
    if ([self.notificationTableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.notificationTableView setLayoutMargins:UIEdgeInsetsMake(0,70,0,0)];
    }
}

- (void)setViews
{
    self.notificationTableView = [[UITableView alloc] init];
    [self.notificationTableView setDelegate:self];
    [self.notificationTableView setDataSource:self];
    self.notificationTableView.rowHeight = 77.f;
    self.notificationTableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_notificationTableView];
    
    [self.notificationTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)SetNavgationBarTitle
{
    self.titleString = @"全部通知";
    
    self.titlePullView = [[DDTitlePullView alloc] initWithFrame:self.view.frame];
    //self.titlePullView.backgroundColor = [UIColor clearColor];
    self.titlePullView.hidden = YES;
    [self.view addSubview:_titlePullView];
    
    self.titleTouchLabel = [[UILabel alloc] init];
    self.titleTouchLabel.textAlignment = NSTextAlignmentCenter;
    
    self.titleTouchLabel.attributedText = [self setTitleAttributed];
    
    self.titleTouchLabel.frame = CGRectMake(0, 0, 180, 30);
    self.navigationItem.titleView = self.titleTouchLabel;
   
    
    WEAKSELF
    NSArray *titles = @[@"全部通知",@"新发布职位",@"查看过我的微简历",@"对我的微简历感兴趣"];
    self.titlePullView.titles = titles;
    self.titlePullView.seletedTitleBlock = ^(NSInteger idx) {
        weakSelf.titleString = titles[idx];
        weakSelf.titleTouchLabel.attributedText = [weakSelf setTitleAttributed];
        [weakSelf updateWithTypeIdx:idx];
    };
    self.titleTouchLabel.userInteractionEnabled = YES;
    [self.titleTouchLabel bk_whenTapped:^{
        
        if (![weakSelf.titlePullView isShowPullView]) {
            [weakSelf.titlePullView showPullView];
        } else {
            [weakSelf.titlePullView dismissPullView];
        }
        weakSelf.titleTouchLabel.attributedText = [weakSelf setTitleAttributed];
        
    }];
    
    [self updateWithTypeIdx:0];
}

- (void)updateWithTypeIdx:(NSInteger)idx
{
    switch (idx) {
        case 0:
            _notificationType = DDJobfinderNotificationAll;
            break;
        case 1:
            _notificationType = DDJobfinderNotificationNewJobs;
            break;
        case 2:
            _notificationType = DDJobfinderNotificationBeLooked;
            break;
        case 3:
            _notificationType = DDJobfinderNotificationBeInterested;
            break;
        default:
            break;
    }
    [self reloadData];
}


- (NSMutableAttributedString *)setTitleAttributed
{
    NSMutableAttributedString *titleAttributed = [[NSMutableAttributedString alloc] initWithString:self.titleString];
    [titleAttributed addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0,self.titleString.length)];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    if ([self.titlePullView isShowPullView]) {
        textAttachment.image = [UIImage imageNamed:@"pull_up"];
    } else {
        textAttachment.image = [UIImage imageNamed:@"pull_down"];
    }
    
    textAttachment.bounds = CGRectMake(5, 2, 9, 5);
    [titleAttributed insertAttributedString:[NSAttributedString attributedStringWithAttachment:textAttachment] atIndex:self.titleString.length];
    return titleAttributed;
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

#pragma mark --- UITableView DataSourcw

- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.notifications.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndentifier = @"cell";
    DDNotificationMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (!cell) {
        cell = [[DDNotificationMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    if (indexPath.row < self.notifications.count)
    {
        DDJobfinderNotification *notification = [self.notifications objectAtIndex:indexPath.row];
        [cell setModel:notification];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.titlePullView.isShowPullView) {
        [self.titlePullView dismissPullView];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row < self.notifications.count)
    {
        DDJobfinderNotification *notification = [self.notifications objectAtIndex:indexPath.row];
        
        DDJobsDetail *jobsDetial = [[DDJobsDetail alloc] init];
        jobsDetial.jobid = notification.jobid;
        DDWorkDetailViewController *vc = [[DDWorkDetailViewController alloc] init];
        vc.jobDetail = jobsDetial;
        [self.navigationController pushViewController:vc animated:YES];
        
//        LDJobfinderDetailVC *vc = [[LDJobfinderDetailVC alloc] init];
        
    }
}

#pragma mark - functions

- (void)reloadData
{
    [self.notifications removeAllObjects];
    self.page = 0;
    [self loadData];
}

- (void)loadMore
{
    self.page += 1;
    [self loadData];
}

- (void)loadData
{
    [self showLoading];
    NSString *url = [[DDAPIGenerate sharedInstance] API:@"jobfinder_queyr_notifications"];
    __weak typeof(self) weakSelf = self;
    
    [[DDNetWorkManager shareManager] POST:url parameters:@{@"type":@(_notificationType)} success:^(id responseObject) {
        [weakSelf hideLoading];
        if ([weakSelf isValidateResponseData:responseObject])
        {
            NSDictionary *data = responseObject[@"data"];
            [weakSelf updateWithData:data];
        }
        [weakSelf.notificationTableView reloadData];
    } failure:^(id responseObject, NSError *error) {
        [weakSelf responseUnValidate:responseObject];
    }];
}

- (void)updateWithData:(NSDictionary *)data
{
    self.hasMore = data[@"hasNextPage"];
    if (self.hasMore) {
        NSArray *notifications = data[@"notifications"];
        NSArray *models = [NSArray yy_modelArrayWithClass:[DDJobfinderNotification class] json:notifications];
        [self.notifications addObjectsFromArray:models];
        [self.notificationTableView reloadData];
    }
}

- (NSMutableArray *)notifications
{
	if (!_notifications){
        _notifications = [NSMutableArray new];
	}
	return _notifications;
}

@end
